﻿//********************************************************************
// 
//  file:  frmReplicate.cs
// 
//  (C) Copyright 2010, Diomede Corporation
//  All rights reserved
// 
//  Use, modification, and distribution is subject to   
//  the New BSD License (See accompanying file license.txt).
// 
//********************************************************************

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Diomede.SDK;
using data.diomedestorage.com;

namespace Diomede.Explorer
{
    public partial class frmReplicate : Form
    {
        public frmReplicate()
        {
            InitializeComponent();
        }

        public frmReplicate(ISession session)
        {
            InitializeComponent();
            Session = session;
        }

        public ISession Session { get; set; }

        public long FileID { get; set; }

        private void frmReplicate_Load(object sender, EventArgs e)
        {
            IFileManagement f = Session as IFileManagement;

            IGetStorageTypesExecResult result = f.StoredFileInstance(FileID).GetStorageTypes();

            if (result.Status == Diomede.SDK.Enums.ExecutionResults.Success)
            {
                foreach(StorageType type in result.StorageTypes)
                {
                    lstStorageType.Items.Add( string.Format("{0} - {1} ({2})", type.storageTypeID, type.name, type.powerPerMB));
                }
                lstStorageType.SelectedIndex = 0;
                dteExpirationDate.Value = DateTime.Now.AddDays(1);
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            IFileManagement f = Session as IFileManagement;

            byte storageType = Convert.ToByte(lstStorageType.SelectedItem.ToString().Substring(0,1));

            IReplicateExecResult result;
            if (chkExpiration.Checked)
                result = f.StoredFileInstance(FileID).Replicate(FileID, storageType, dteExpirationDate.Value);
            else
                result = f.StoredFileInstance(FileID).Replicate(FileID, storageType, null);

            if (result.Status == Diomede.SDK.Enums.ExecutionResults.Success)
            {
                MessageBox.Show(this, "Replication successful.\n\nPhysical File ID: " + result.PhyFileID.ToString(), "Replicate File");
                Close();
            }
            else
            {
                MessageBox.Show(this, result.Message, "Replicate File");
                return;
            }
        }

        private void chkExpiration_CheckedChanged(object sender, EventArgs e)
        {
            dteExpirationDate.Enabled = chkExpiration.Checked;
        }
    }
}
